package com.android.binderserverclient1
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import androidx.appcompat.widget.LinearLayoutCompat
import binderipc.PassValueProxy
import binderipcremoteviews.TranscationRemoteViewsProxy
import binderipcservicemanager.AdvancedServiceManagerConstant
import binderipcservicemanager.BCAdvancedServiceManagerProxy
import binderipcservicemanager.IBCAdvancedServiceManager
import com.android.binderserverclient1.databinding.ActivityShowRemoteViewsBinding

class ShowRemoteViewsActivity : BaseActivity<ActivityShowRemoteViewsBinding>() {

    override fun getLayoutId() = R.layout.activity_show_remote_views

    override fun init(savedInstanceState: Bundle?) {

        var mBCServiceManager: IBCAdvancedServiceManager? = null

        val bundleResult = contentResolver.call(
            Uri.parse(AdvancedServiceManagerConstant.SERVICE_MANAGER_URI),
            AdvancedServiceManagerConstant.ACTION_ADVANCED_GET_SERVICE,
            null,
            null
        )

        if (bundleResult != null) {
            val binder = bundleResult.getBinder(AdvancedServiceManagerConstant.KEY_ADVANCED_SERVER)
            if (binder != null) {
                mBCServiceManager = BCAdvancedServiceManagerProxy(binder)
            }
        }

        Log.d(TAG,"${mBCServiceManager?.getService("TranscationRemoteViewsService") == null}")
        val mTransactionService = mBCServiceManager?.getService("TranscationRemoteViewsService")
            ?.let { TranscationRemoteViewsProxy(it) }

        //RemoteViews apply()用于添加view   reapply()用于更新view

        val view = mTransactionService?.addRemoteViews()?.apply(this@ShowRemoteViewsActivity, null)
        view?.let {
            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                this@ShowRemoteViewsActivity.resources.getDimension(R.dimen.dp_100).toInt()
            )
            mBinding.showRemoteviewsRoot.addView(it, params)
        }
    }

    companion object {
        const val TAG = "ShowRemoteViewsActivity"
    }
}